package minesweeper;

import java.util.Random;

public class DynamicMinesweeper extends Minesweeper {
	
	public static DynamicMinesweeper createMinesweeperData(int rows, int columns, int firstPressedRow, int firstPressedColumn) {
		if(rows > 0 && columns > 0) {
			DynamicMinesweeper minesweeper = new DynamicMinesweeper(rows, columns);
			minesweeper.createField(firstPressedRow, firstPressedColumn); //??? nice -> possible
			return minesweeper;
		}
		else
			return null;
	}
	
	protected DynamicMinesweeper(int rows, int columns) {
		super(rows, columns);
	}
	
	private void createField(int firstPressedRow, int firstPressedColumn) {
		putMines(firstPressedRow, firstPressedColumn);
		putSavingBlocks();
	}
	
	private void putMines(int firstPressedRow, int firstPressedColumn) {
		Random rnd = new Random();
		int row = -1;
		int col = -1;
		for(int i = 1; i <= NUMBER_OF_MINES; i++) {
			row = rnd.nextInt(minesweeperData.length); // get random row
			col = rnd.nextInt(minesweeperData[row].length); // get random column
			
			//check if there already is a mine
			if(isInFirstPressedRange(row, col, firstPressedRow, firstPressedColumn) || !putMine(row, col)) i--; // try again
		}
	}

	//added condition
	private boolean isInFirstPressedRange(int row, int column, int firstPressedRow,
			int firstPressedColumn) {
		
		return row >= (firstPressedRow - 1) 
				&& row <= (firstPressedRow + 1)
				&& column >= (firstPressedColumn - 1)
				&& column <= (firstPressedColumn + 1);
	}

}